/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;

namespace Xceed.Silverlight.Data
{
  internal class SyncParentEnumerator<T> : SyncEnumerator<T>
  {
    protected SyncParentEnumerator( ISyncEnumerator childEnumerator )
    {
      this.ChildEnumerator = childEnumerator;
    }


    #region ChildEnumerator Property

    internal ISyncEnumerator ChildEnumerator
    {
      get
      {
        return m_childEnumerator;
      }
      set
      {
        if( m_childEnumerator != value )
        {
          if( m_childEnumerator != null )
          {
            m_childEnumerator.DataChanged -= new EventHandler<DataChangedEventArgs>( ChildDataChanged );
          }

          m_childEnumerator = value;

          if( m_childEnumerator != null )
          {
            m_childEnumerator.DataChanged += new EventHandler<DataChangedEventArgs>( ChildDataChanged );
          }
        }
      }
    }

    #endregion ChildEnumerator Property

    #region ChildEnumeratorTyped Property

    internal ISyncEnumerator<T> ChildEnumeratorTyped
    {
      get
      {
        return ( ISyncEnumerator<T> )m_childEnumerator;
      }
    }

    #endregion ChildEnumeratorTyped Property


    protected virtual void ChildDataChanged( object sender, DataChangedEventArgs args )
    {
      this.RaiseDataChanged( args );
    }


    #region IDisposable Members

    private bool m_disposed;//false

    protected override void Dispose( bool disposing )
    {
      if( !m_disposed )
      {
        try
        {
          if( disposing )
          {
            if( m_childEnumerator != null )
            {
              m_childEnumerator.DataChanged -= ChildDataChanged;
            }

            if( m_childEnumerator is IDisposable )
            {
              ( ( IDisposable )m_childEnumerator ).Dispose();
            }
          }
          m_disposed = true;
        }
        finally
        {
          base.Dispose( disposing );
        }
      }
    }

    #endregion IDisposable Members

    #region ISyncEnumerator<T> Members

    public override bool JumpMoveAndFetch(
      JumpMoveAndFetchParameters parameters,
      out Nullable<long> actualMoveCount, 
      out ICollection<T> fetchedItems, 
      out SourcePosition endPosition,
      out RelativeOffset endPositionOffset,
      out EstimateInt64 totalCount )
    {
      return this.ChildEnumeratorTyped.JumpMoveAndFetch(
               parameters,
               out actualMoveCount,
               out fetchedItems,
               out endPosition,
               out endPositionOffset,
               out totalCount );
    }

    #endregion ISyncEnumerator<T> Members

    #region ISyncEnumerator Members

    public override GroupCountResult GroupCount( CountParameters parameters )
    {
      return this.ChildEnumerator.GroupCount( parameters );
    }

    public override CountResult Count( CountParameters parameters )
    {
      return this.ChildEnumerator.Count( parameters );
    }

    public override StatResults GetStatValues( StatParameters parameters )
    {
      return this.ChildEnumerator.GetStatValues( parameters );
    }

    public override bool JumpMoveAndFetch(
      JumpMoveAndFetchParameters parameters,
      out Nullable<long> actualMoveCount, 
      out ICollection fetchedItems, 
      out SourcePosition endPosition,
      out RelativeOffset endPositionOffset,
      out EstimateInt64 totalCount )
    {
      return this.ChildEnumeratorTyped.JumpMoveAndFetch(
               parameters,
               out actualMoveCount,
               out fetchedItems,
               out endPosition,
               out endPositionOffset,
               out totalCount );
    }

    #endregion ISyncEnumerator Members


    #region Private Fields

    private ISyncEnumerator m_childEnumerator; //null

    #endregion Private Fields
  }
}
